<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="wilmar" uri="/wilmar-process"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>authorization undo</title>
<script type="text/javascript">
	var i = 0;

	function createRows() {

		var table = document.getElementById("table2");
		var tRows = table.rows;
		if(i == 0){
				i = document.getElementById("table2").rows.length - 2;
			}
		
		var row = table.insertRow(1);
		row.id = i;
		var rCells = row.cells;
		var rCell_1 = tRows[row.rowIndex].insertCell(rCells.length);
		var rCell_2 = tRows[row.rowIndex].insertCell(rCells.length);
		var rCell_3 = tRows[row.rowIndex].insertCell(rCells.length);
		var rCell_4 = tRows[row.rowIndex].insertCell(rCells.length);

		rCell_1.innerHTML = "<input type='text' id='orgValue' name=processAuthorityList[" + i + "].organizationName readonly='true'></input><input type='hidden' name=processAuthorityList["
				+ i
				+ "].organizationCode id='orgKey' readonly='true'></input><input type='hidden' name=processAuthorityList["
				+ i
				+ "].processDefinitionKey id='proDefKey' readonly='true'></input>";
		rCell_2.innerHTML = "<input type='text' id='roleValue' name=processAuthorityList[" + i + "].roleName readonly='true'></input><input type='hidden' name=processAuthorityList["
				+ i + "].roleCode id='roleKey' readonly='true'></input>";
		rCell_3.innerHTML = "<input type='text' id='authorityTypeValue' readonly='true'></input><input type='hidden' name=processAuthorityList["
				+ i
				+ "].authorizeType id='authorityTypeKey' readonly='true'></input>";
		rCell_4.innerHTML = "<input type='button' name=" + i
				+ " onclick='delRow(this);' value='<s:text name=\"global.button.releal\" />'></input>";

		setValue();

		i++;
	}

	function delRow(button) {
		// i--;
		var row = document.getElementById(button.name);
		var table = document.getElementById("table2");
		table.deleteRow(row.rowIndex);
	}

	function setValue() {
		// 获得选中项
		var authoritySelected = document.getElementById("authoritySelected");

		var aSelectedIndex = authoritySelected.selectedIndex;
		var aSelectedKey = authoritySelected.options[aSelectedIndex].value;
		var aSelectedValue = authoritySelected.options[aSelectedIndex].text;

		var orgSelected = document.getElementById("orgSelected");

		var oSelectedIndex = orgSelected.selectedIndex;
		var oSelectedKey = orgSelected.options[oSelectedIndex].value;
		var oSelectedValue = orgSelected.options[oSelectedIndex].text;

		var roleSelected = document.getElementById("roleSelected");

		var rSelectedIndex = roleSelected.selectedIndex;
		var rSelectedKey = roleSelected.options[rSelectedIndex].value;
		var rSelectedValue = roleSelected.options[rSelectedIndex].text;

		// 赋值 

		var proDefKey = document.getElementById("proDefKey");
		var proceDefKeyHidden = document.getElementById("proceDefKeyHidden");
		proDefKey.value = proceDefKeyHidden.value;

		var orgValue = document.getElementById("orgValue");
		var orgKey = document.getElementById("orgKey");

		var roleValue = document.getElementById("roleValue");
		var roleKey = document.getElementById("roleKey");

		var authorityTypeValue = document.getElementById("authorityTypeValue");
		var authorityTypeKey = document.getElementById("authorityTypeKey");

		if (aSelectedKey == "PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ORGANIZATION") {
			orgValue.value = oSelectedValue;
			orgKey.value = oSelectedKey;
			roleValue.value = "";
			roleKey.value = "";
		} else if (aSelectedKey == "PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ROLE") {
			roleValue.value = rSelectedValue;
			roleKey.value = rSelectedKey;
			orgValue.value = "";
			orgKey.value = "";
		} else if (aSelectedKey == "PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_BOTH") {
			orgValue.value = oSelectedValue;
			orgKey.value = oSelectedKey;

			roleValue.value = rSelectedValue;
			roleKey.value = rSelectedKey;
		}

		authorityTypeValue.value = aSelectedValue;
		authorityTypeKey.value = aSelectedKey;
	//	alert('org ' + orgValue.value + '   ' + orgKey.value + ' role '
	//			+ roleValue.value + '    ' + roleKey.value);

	}

	function change(selected) {

		var index = selected.selectedIndex;
		var selectedValue = selected.options[index].value;
		//	alert('selected index: ' + index + '\nselected value: ' + selectedValue
		//			+ '\nselected text: ' + selected.options[selected.selectedIndex].text);
		var orgDiv = document.getElementById("orgDiv");
		var roleDiv = document.getElementById("roleDiv");
		switch (index) {
		case 0:
			//	var orgDiv = document.getElementById("orgDiv");
			orgDiv.style.display = "block";
			if (roleDiv.style.display == "block") {
				roleDiv.style.display = "none";
			}
			break;
		case 1:
			//	var roleDiv = document.getElementById("roleDiv");
			roleDiv.style.display = "block";
			if (orgDiv.style.display == "block") {
				orgDiv.style.display = "none";
			}
			break;
		case 2:
			orgDiv.style.display = "block";
			roleDiv.style.display = "block";
			break;
		}
	}

	function undo(button) {
		var table = document.getElementById("table2");
		// alert(button.name);
		var row = document.getElementById(button.name);
		// alert('button.name  : ' + button.name + '\n row.rowIndex : ' + row.rowIndex);
		table.deleteRow(row.rowIndex);
		
	}
</script>

</head>
<body>
<center><wilmar:process.logo /></center>
<table id="table1" align="left" cellspacing="0" cellpadding="0"
	 border="0">
	<tr style="display: block">
		<td><s:text name="page.title.authorization.type" />:</td>
		<td><s:select id="authoritySelected" onchange="change(this);"
			list="#{'PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ORGANIZATION':'对机构授权', 'PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_ROLE':'对角色授权', 'PROCESS_SYSTEM_DEFINITION_AUTHORITY_TYPE_BOTH':'对两者授权'}"></s:select></td>
	</tr>
	<tr style="display: block" id="orgDiv">
		<td><s:text name="page.title.authorization.type.org" />:</td>
		<td><s:select id="orgSelected"
			list="#{'0':'丰益(上海)信息技术有限公司','1':'益海嘉里集团','2':'益海嘉里投资公司','3':'南海油脂工业(赤湾)有限公司','4':'丰益（佳木斯）食品工业有限公司','5':'益海嘉里营销长沙分公司'}"></s:select></td>
	</tr>
	<tr style="display: none" id="roleDiv">
		<td><s:text name="page.title.authorization.type.role" />:</td>
		<td><s:select id="roleSelected"
			list="#{'0':'r0','1':'r1','2':'r2','3':'r3','4':'r4','5':'r5'}"></s:select></td>
	</tr>
	<tr>
		<td colspan="2" align="left"><input type="button" value="add"
			onClick=
	createRows();;
></input></td>
	</tr>

</table>
	<br/>
	<br/>
	<br/>
	<br/>
	<br/>

<s:form namespace="/process"
	action="processDefinitionAction!doUndoAuthorition.action" method="post">
	<s:hidden value="%{processAuthority.processDefinitionKey}"
		id="proceDefKeyHidden" name="processAuthority.processDefinitionKey"></s:hidden>
	<table id="table2" width="50%" border="0" align="center"
		cellpadding="0" cellspacing="1" bgcolor="#f9f9f9">
		<tr id="more">
			<td><s:text name="page.title.authorization.type.org" /></td>
			<td><s:text name="page.title.authorization.type.role" /></td>
			<td><s:text name="page.title.authorization.type" /></td>
			<td><s:text name="table.title.actions" /></td>
		</tr>
		<s:iterator value="%{processAuthorityVOList}" status="voList"
			id="processAuthorityVOList" var="procAuth">
			<tr id="<s:property value='%{#voList.index}'/>">
				<td><s:textfield
					name="processAuthorityList[%{#voList.index}].organizationName"
					readonly="true"
					value="%{processAuthorityVOList[#voList.index].organizationName}"></s:textfield><s:hidden
					name="processAuthorityList[%{#voList.index}].processDefinitionKey"
					value="%{processAuthorityVOList[#voList.index].processDefinitionKey}"></s:hidden><s:hidden
					name="processAuthorityList[%{#voList.index}].organizationCode"
					value="%{processAuthorityVOList[#voList.index].organizationCode}"></s:hidden></td>
					
				<td><s:textfield
					name="processAuthorityList[%{#voList.index}].roleName"
					value="%{processAuthorityVOList[#voList.index].roleName}"
					readonly="true"></s:textfield><s:hidden
					name="processAuthorityList[%{#voList.index}].roleCode"
					value="%{processAuthorityVOList[#voList.index].roleCode}"></s:hidden></td>
				<td><s:textfield
					name="processAuthorityList[%{#voList.index}].authorizeTypeName"
					value="%{processAuthorityVOList[#voList.index].authorizeTypeName}"
					readonly="true"></s:textfield><s:hidden
					name="processAuthorityList[%{#voList.index}].authorizeType"
					value="%{processAuthorityVOList[#voList.index].authorizeType}"></s:hidden></td>
				<td><input type="button" name="<s:property value='%{#voList.index}'/>" value="%{getText('global.button.repeal')}" onclick="undo(this);"></input></td>
			</tr>
		</s:iterator>
		<tr>
			<td colspan="4"><s:submit></s:submit></td>
		</tr>
	</table>
</s:form>

</body>
</html>